with keyword_parser; use keyword_parser;
with config;  use config; --
with GUI;     use GUI;
with GUI.Log; use GUI.Log;
with managed;
with ffxilog_interface.encoding; use ffxilog_interface.encoding;
with Ada.Calendar;

package body process_line is
  procedure process(Main_Window : in out GUI.FFXIAlarm_Window_Type;
                    Log_Window  : in out GUI.Log.Log_Window_Type;
                    Line        : in Log_Line;
                    Alarm       : out Integer) is
    text : constant wide_string := DecodeUTF8(line.text);
    config : constant config_type := Managed.Config.Retrieve;

    function Search_Keywords return Integer is
    begin
      for i in config.keywords'range loop
        if config.keywords(i).set then
          if config.keywords(i).length>0
            and parse_line(config.keywords(i).word(1..config.keywords(i).length),
                           text) then
            Notify(Main_Window, "Word found at ");
            return i;
          end if;
        end if;
      end loop;
      return -1;
    end Search_Keywords;

    procedure Process_Type is
    begin
      case line.whatis is
        when iTell =>
          if config.tell then
            Notify(Main_Window, "Tell received at ");
            Alarm := 0;
          end if;
        when iLinkshell =>
          if config.linkshell then
            Notify(Main_Window, "Linkshell message received at ");
            Alarm := 0;
          end if;
        when iParty =>
          if config.party then
            Notify(Main_Window, "Party message received at ");
            Alarm := 0;
          end if;
        when iSay =>
          if config.say then
            Notify(Main_Window, "Say received at ");
            Alarm := 0;
          end if;
        when iShout =>
          if config.shout then
            Notify(Main_Window, "Shout received at ");
            Alarm := 0;
          end if;
        when others =>
          null;
      end case;
    end Process_type;

  begin
    alarm := Search_Keywords;
    if alarm = -1 then
      Process_Type;
    end if;
    Log_Event(Log_Window, alarm  /= -1, Ada.Calendar.Clock, line.Whatis, text);
  end process;
end process_line;
